// 安装gulp插件

//引入使用的模块
const gulp = require("gulp"),
 htmlmin = require("gulp-htmlmin"),
 minifyCss = require("gulp-clean-css"),
 babel = require("gulp-babel"),
 uglify = require("gulp-uglify"),
 connect = require("gulp-connect");

 //定制任务
 //压缩html
 gulp.task("html",function(){
   gulp.src("src/**/*.html")
   .pipe(htmlmin({ collapseWhitespace: true }))
   .pipe(gulp.dest("dist/"))
   .pipe(connect.reload());
 });

 //压缩css
 gulp.task("css",()=>{
     gulp.src("src/css/*.css")
     .pipe(minifyCss())
     .pipe(gulp.dest("dist/css"))
     .pipe(connect.reload());
 });

 //压缩JS
 gulp.task("js",()=>{
     gulp.src("src/js/*.js")
     .pipe(babel({
        presets: ['env']
    }))
    .pipe(uglify())
     .pipe(gulp.dest("dist/js"))
     .pipe(connect.reload());
 });

 //复制任务
 gulp.task("copy-images",()=>{
     gulp.src("src/images/**/*.*")
     .pipe(gulp.dest("dist/images"));
 });

 gulp.task("copy-lib",()=>{
    gulp.src("src/lib/**/*.*")
    .pipe(gulp.dest("dist/lib"));
});
gulp.task("copy",["copy-images","copy-lib"]);

//webserver
gulp.task("webserver",()=>{
    connect.server({
        root: 'dist',
        livereload: true})
})

//watch
gulp.task("watch",()=>{
    gulp.watch("src/**/*.html",["css"]);
    gulp.watch("sec/js/*.js",["js"]);
    gulp.watch("src/css/*.css",["css"]);
})

//default任务
gulp.task("default",["css","js","copy","webserver","html","watch"]);
